<?php namespace App\Http\Controllers;

use App\Answers;
use App\Questions;
use Illuminate\Http\Request;

class QuestionsController extends Controller
{
    public function index()
    {
        $allQuestions = Questions::all();

        $questionArry = array();
        foreach ($allQuestions as $question) {
            array_push($questionArry, $question);
        }
        $lenth = count($allQuestions);
        $num = -1;
        if ($lenth > 0) {
            $num = mt_rand(0, $lenth - 1);
        }

        $result = null;
        if ($num != -1) {
            $result = $questionArry[$num];
        }

        $answers = null;
        if(!is_null($result)){
            $thatAnswers = Answers::whereQid($result['id'])->get();
            $answers = $thatAnswers;
        }

        return view('question', ['question' => $result,'answers'=>$answers]);
    }

    public function analys(Request $request){
        $qid = $request->get('qid');
        $thatQuestion = Questions::whereId($qid)->first();

        $analys = null;
        if($thatQuestion){
            $analys = $thatQuestion->analyse;
        }
        return view('analys',['analys'=>$analys]);
    }
}